package com.huobao.yunying.activitys;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Ketang_more;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.Fragment_ketang_jiangshi;
import com.huobao.yunying.entity.Fragment_ketang_jieshao;
import com.huobao.yunying.entity.KetangGrid;
import com.huobao.yunying.entity.KetangMore;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.yunying.utils.Util;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.yunying.views.RoundImageView;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ViewInject;

import java.util.List;
import java.util.Map;

public class Act_Ketang_detail extends HuobaoBaseActivity implements OnItemClickListener {

	// @ViewInject(R.id.tabview)
	// private AbSlidingTabView mAbSlidingTabView;
	//
	private KetangMore data;
	private KetangGrid hetanggrid;

	/** 课程名称 */
	private TextView tv_kecheng_name;
	/** 课程简洁 */
	private TextView tv_kecheng_content;
	/** 头像 */
	private RoundImageView imageview;
	/** 姓名 */
	private TextView tv_name;
	/** 介绍 */
	private TextView tv_jieshao;
	private NoScrollListView listview;
	private Adapter_Ketang_more adapter;
	
	@ViewInject(R.id.image)
	private ImageView image;
	@ViewInject(R.id.llyt)
	private LinearLayout llyt;
	
	@ViewInject(R.id.rtly)
	private RelativeLayout layout;
	
	@ViewInject(R.id.scrollView)
	private ScrollView scrollView;

	private String id;
	@Override
	protected int getContentView() {
		// return R.layout.layout_ketang_detail;
		return R.layout.layout_fragment_ketangdetail;
	}

	@Override
	protected String getTitleText() {
		id = getIntent().getStringExtra("id");
		if(id != null)return "";
		data = (KetangMore) getIntent().getSerializableExtra("data");
		hetanggrid = (KetangGrid) getIntent().getSerializableExtra("more");
		return data == null ? hetanggrid.getCourse_title() : data.getCourse_title();
	}

	private void findviews() {
		tv_kecheng_name = (TextView) findViewById(R.id.tv_title);
		tv_kecheng_content = (TextView) findViewById(R.id.tv_content);
		tv_jieshao = (TextView) findViewById(R.id.tv_jieshao);
		tv_name = (TextView) findViewById(R.id.tv_name);
		imageview = (RoundImageView) findViewById(R.id.imageview);
		listview = (NoScrollListView) findViewById(R.id.listview);
	}

	@Override
	protected void init() {
		findviews();
		mAbTitleBar.setVisibility(View.VISIBLE);
		imageview.setRectAdius(90);
		adapter = new Adapter_Ketang_more(this);
		listview.setOnItemClickListener(this);
		listview.setAdapter(adapter);
		if(id == null){
			if(data != null){
				getDetail(data.getCourse_id(),false);
			}else{
				getDetail(hetanggrid.getCourse_id(),false);

			}
		}else{
			getDetail(id,false);
		}
		// 缓存数量
		// mAbSlidingTabView.getViewPager().setOffscreenPageLimit(3);
		// mAbSlidingTabView.setTabTextColor(Color.BLACK);
		// mAbSlidingTabView.setTabSelectColor(Color.rgb(30, 168, 131));
		// mAbSlidingTabView.setTabBackgroundResource(R.drawable.tab_bg);
		// mAbSlidingTabView.setTabLayoutBackgroundResource(R.drawable.slide_top);
		// mAbSlidingTabView.setTabPadding(20, 8, 20, 8);
		//
		// List<Fragment> mFragments = new ArrayList<Fragment>();
		// Fragment_ketang_detail fragment1 =
		// Fragment_ketang_detail.getInstant();
		// fragment1.setid(id);
		// Fragment_ketang_detail fragment2 =
		// Fragment_ketang_detail.getInstant();
		// fragment2.setid(id);
		// Fragment_ketang_detail fragment3 =
		// Fragment_ketang_detail.getInstant();
		// fragment3.setid(id);
		// List<String> tabTexts = new ArrayList<String>();
		// tabTexts.add("详情");
		// tabTexts.add("讲师");
		// tabTexts.add("推荐");
		// mFragments.add(fragment1);
		// mFragments.add(fragment2);
		// mFragments.add(fragment3);
		// mAbSlidingTabView.addItemViews(tabTexts, mFragments);
		// mAbSlidingTabView.setTabPadding(20, 8, 20, 8);
	}

	private void getDetail(String strid, final boolean play) {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "ketang", HttpConst.SECOND_KEY, "kchshow",
				"id", strid);

		Http.getInstant().sendRequest(this, true, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				Fragment_ketang_jieshao jieshao = JSON.parseObject(jsonObject.getString("skecheng"),
						Fragment_ketang_jieshao.class);
				Fragment_ketang_jiangshi jiangshi = JSON
						.parseArray(jsonObject.getString("slecturer"), Fragment_ketang_jiangshi.class).get(0);
				List<KetangMore> list = JSON.parseArray(jsonObject.getString("srelevant"), KetangMore.class);
				mAbTitleBar.setTitleText(jieshao.getCourse_title());
				if(play){
					final String course_link = jieshao.getCourse_link();
					final String str = course_link.substring(course_link.length()-3);
					if(str.equals("mp3")){
						Util.playAudio(context, jieshao.getCourse_link());
					}else if(str.equals("mp4")){
						Util.playVideo(context, jieshao.getCourse_link());
					}
				}else{
					adapter.clear();
					adapter.addAll(list);
					showJieshao(jieshao);
					showJiangshi(jiangshi);
					scrollView.smoothScrollTo(0, 0);
				}
			}

			private void showJiangshi(Fragment_ketang_jiangshi jiangshi) {
				ImageOptions imageOptions = new ImageOptions.Builder()
						.setSize(DensityUtil.dip2px(320), DensityUtil.dip2px(320))// 图片大小
						// .setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
						// .setCrop(true)// 如果ImageView的大小不是定义为wrap_content,
						// 不要crop.
						.setImageScaleType(ImageView.ScaleType.CENTER_CROP)
						.setLoadingDrawableId(R.mipmap.imageloading)// 加载中默认显示图片
						// .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
						.build();
				ImageUtils.loadimg(imageview, imageOptions, jiangshi.getLecturer_img(), null);
				tv_name.setText(jiangshi.getLecturer_name());
				tv_jieshao.setText(jiangshi.getLecturer_intro());
			}

			private void showJieshao(Fragment_ketang_jieshao jieshao) {
				tv_kecheng_name.setText(jieshao.getCourse_title());
				tv_kecheng_content.setText(jieshao.getCourse_intro());
				mAbTitleBar.setTitleText(jieshao.getCourse_title());
				final String course_link = jieshao.getCourse_link();
				final String str = course_link.substring(course_link.length()-3);
				if(str.equals("mp3")){
					layout.setVisibility(View.GONE);
				}else if(str.equals("mp4")){
					layout.setVisibility(View.VISIBLE);
					ImageUtils.loadimg(image, jieshao.getCourse_spimg(), new OnImageLoadListener() {
						
						@Override
						public void onLoadSuccess(Drawable drawable) {
							int newWidth = ScreenUtil.getInstant().getDisWidth(context);
							int newHight = newWidth * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
							LayoutParams layoutParams = (LayoutParams) layout.getLayoutParams();
							layoutParams.width = newWidth;
							layoutParams.height = newHight;
							layout.setLayoutParams(layoutParams);
						}
					});
					layout.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							//OpenIntentUtil.openVideo(course_link, context);	
							Util.playVideo(context, course_link);
						}
					});
				}
				
				llyt.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						if(str.equals("mp3")){
							Util.playAudio(context, course_link);
						}else if(str.equals("mp4")){
							Util.playVideo(context, course_link);
						}
					}
				});
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		getDetail(adapter.getList().get(position).getCourse_id(), true);
	}

}
